import axios, { AxiosResponse, AxiosRequestConfig } from "axios";

// http://www.wwlib.cn/index.php/api/llmv2
export const BASE_URL = 'http://www.wwlib.cn/index.php'// 'http://www.wwlib.cn/index.php/'
export const service = axios.create({
  baseURL: BASE_URL, //
  timeout: 5000,
  withCredentials: true,
  headers: { "Content-Type": "application/json;charset=utf-8" },
}); // Request interceptors

service.interceptors.request.use(
  (config: any) => {
    // 设置请求头
    config.headers = {
      "Content-Type": "application/json;charset=utf-8",
      'aaa': '1',
      ...config.headers,
      // 'Cookie':'PHPSESSID=6qnv2aig1lgjgu4br98l3sc375; Hm_lvt_9534de394d0c2b1e6b6b09d9bb2a6eb0=1733815939,1733986588,1734064211,1734235395; HMACCOUNT=7CEEDFF089BB8FD5; Hm_lpvt_9534de394d0c2b1e6b6b09d9bb2a6eb0=1734250690'
    };
    console.log(config);
    
    // do something
    return config;
  },
  (error: any) => {
    Promise.reject(error);
  },
); // Response interceptors

service.interceptors.response.use(
  async (response: AxiosResponse) => {
    console.log('响应拦截', response.data);
    if (Number(response?.data?.code) === 201) {
      // 重定向网页到http://www.wwlib.cn/index.php/login
      window.location.href = 'http://www.wwlib.cn/index.php/login';
    }
    if (Number(response?.data?.code) === 4409) {
      window.location.href = 'http://www.wwlib.cn/index.php/home/page/wallet';
    }
    return response.data;
  },
  (error: any) => {
    // do something
    return Promise.reject(error);
  },
);

export default service;
